﻿Public Class MetaApplication
    Implements IDisposable

    Private disposedValue As Boolean = False        ' To detect redundant calls
    Private _metaApplicationOptions As MetaApplicationOptions = New MetaApplicationOptions()
    Private mq As MetaQuery

    Public Property ApplicationID As Guid
    Public Property AppName As String
    Public Property SystemRecord As Boolean

    Public ReadOnly Property MetaApplicationOptions() As MetaApplicationOptions
        Get
            Return _metaApplicationOptions
        End Get
    End Property
    ''' <summary>
    ''' Application class that contains, all the value and the description of the ApplicationOption.
    ''' </summary>
    ''' <param name="appName"></param>
    ''' <remarks>Populate by the MetaCommand Metalists</remarks>
    Public Sub New(ByVal appName As String)
        Me.AppName = appName
        Me.FillMetaApplication()
        Me.FillMetaApplicationOption()
    End Sub

    Public Sub FillMetaApplication()
        mq = MetaQuery.GetMetaQueryByName("MetaApplications")
        mq.SQLWhere = String.Format("AppName = '{0}'", Me.AppName)
        mq.FillResultQuery()
        For Each row As DataRow In mq.ResultQuery.Rows
            Me.ApplicationID = CType(row("ApplicationID"), Guid)
            Me.AppName = row("AppName").ToString
            Me.SystemRecord = CBool(row("SystemRecord"))
        Next
    End Sub

    Public Sub FillMetaApplicationOption()
        Dim mao As MetaApplicationOption
        'Dim gid As Guid

        mq = MetaQuery.GetMetaQueryByName("MetaApplicationOptions")
        mq.SQLWhere = String.Format("ApplicationID_FK = '{0}'", Me.ApplicationID)
        mq.FillResultQuery()
        For Each row As DataRow In mq.ResultQuery.Rows
            mao = New MetaApplicationOption()

            mao.AppOptionId = CType(row("AppOptionId"), Guid)
            mao.ApplicationID_FK = CType(row("ApplicationID_FK"), Guid)
            mao.OptionDescr = row("OptionDescr").ToString
            mao.OptionName = row("OptionName").ToString
            mao.DfltOptionValue = row("DfltOptionValue").ToString
            mao.Notes = row("Notes").ToString
            mao.Overwritable = CBool(row("Overwritable"))
            mao.Readonly = CBool(row("Readonly"))
            mao.SystemRecord = CBool(row("SystemRecord"))

            'gid = New Guid(row("ListValueID").ToString)
            If Not Me._metaApplicationOptions.ContainsKey(mao.OptionName) Then
                Me._metaApplicationOptions.Add(mao.OptionName, mao)
            End If
        Next

    End Sub
    

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
                _metaApplicationOptions = Nothing
                mq = Nothing
            End If
            ' TODO: free your own state (unmanaged objects).
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

Public Class MetaApplicationOption

    Public Property AppOptionId As Guid
    Public Property ApplicationID_FK As Guid
    Public Property OptionName As String
    Public Property OptionDescr As String
    Public Property DfltOptionValue As String
    Public Property Notes As String
    Public Property Overwritable As Boolean
    Public Property [Readonly] As Boolean
    Public Property SystemRecord As Boolean

    Public Sub New()
    End Sub


End Class

Public Class MetaApplicationOptions
    Inherits Dictionary(Of String, MetaApplicationOption)

End Class

